package com.shop.cereshop.app.service.redis.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.shop.cereshop.app.service.redis.LabelService;
import com.shop.cereshop.app.utils.BusinessUtils;
import com.shop.cereshop.commons.domain.calculate.Calculate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 店铺和商品标签服务实现类
 * @author zdd
 * @date 2025/11/4 14:22
 * @version 1.0
 */
@Slf4j
@Service
public class LabelServiceImpl implements LabelService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 获取店铺服务标签
     *
     * @param shopIds 店铺id集合
     * return 店铺服务标签
     */
    public Map<Long, Calculate> getShopServiceLabelMap(List<Long> shopIds) {
        List<String> ids = shopIds.stream().map(String::valueOf).collect(Collectors.toList());
        // 批量获取店铺服务标签
        List<Object> labels = redisTemplate.opsForHash().multiGet("shop", ids);
        if (CollUtil.isEmpty(labels)) {
            return Collections.emptyMap();
        }
        // 根据shopId转成Map，同时处理可能的null值和类型转换
        Map<Long, Calculate> labelMap = new HashMap<>();
        for (int i = 0; i < labels.size(); i++) {
            Object label = labels.get(i);
            if (label != null) {
                try {
                    Calculate calculate = JSONUtil.toBean(JSON.toJSONString(label), Calculate.class);
                    if (calculate != null && calculate.getShopId() != null) {
                        labelMap.put(calculate.getShopId(), calculate);
                    }
                } catch (Exception e) {
                    // 记录单个转换异常，不影响其他数据处理
                    log.warn("Failed to convert label for shopId: {}", shopIds.get(i), e);
                }
            }
        }
        return labelMap;
    }

    /**
     * 获取店铺服务标签
     *
     * @param shopId 店铺id
     * return 店铺服务标签
     */
    public Calculate getShopServiceLabel(Long shopId) {
        Object obj = redisTemplate.opsForHash().get("shop", String.valueOf(shopId));
        if (obj == null) {
            return null;
        }
        return JSONUtil.toBean(JSONObject.toJSONString(obj), Calculate.class);
    }

    /**
     * 获取商品标签
     *
     * @param productIds 店铺id集合
     * return 商品标签
     */
    public Map<Long, Calculate> getProductLabelMap(List<Long> productIds) {
        List<String> ids = productIds.stream().map(String::valueOf).collect(Collectors.toList());
        // 批量获取商品服务标签
        List<Object> labels = redisTemplate.opsForHash().multiGet("product", ids);
        if (CollUtil.isEmpty(labels)) {
            return Collections.emptyMap();
        }
        // 根据productId转成Map，同时处理可能的null值和类型转换
        Map<Long, Calculate> labelMap = new HashMap<>();
        for (int i = 0; i < labels.size(); i++) {
            Object label = labels.get(i);
            if (label != null) {
                try {
                    Calculate calculate = JSONUtil.toBean(com.alibaba.fastjson2.JSON.toJSONString(label), Calculate.class);
                    if (calculate != null && calculate.getProductId() != null) {
                        labelMap.put(calculate.getProductId(), calculate);
                    }
                } catch (Exception e) {
                    // 记录单个转换异常，不影响其他数据处理
                    log.warn("Failed to convert label for productId: {}", productIds.get(i), e);
                }
            }
        }
        return labelMap;
    }

    /**
     * 获取商品标签
     *
     * @param productId 商品id
     * return 商品标签
     */
    public Calculate getProductLabel(Long productId) {
        Object obj = redisTemplate.opsForHash().get("product", String.valueOf(productId));
        if (obj == null) {
            return null;
        }
        return JSONUtil.toBean(JSONObject.toJSONString(obj), Calculate.class);
    }

}
